package org.news.web.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.news.web.constant.AuthorInfo;
import org.news.web.domain.vo.CategoriesVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.news.web.domain.vo.TagsVo;
import org.news.web.domain.bo.TagsBo;
import org.news.web.service.ITagsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 标签
 *
 * @author Gu Ci
 * @date 2024-03-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/tags")
public class TagsController extends BaseController {

    private final ITagsService tagsService;

    /**
     * 查询标签列表
     */
    @SaCheckPermission("web:tags:list")
    @GetMapping("/list")
    public TableDataInfo<TagsVo> list(TagsBo bo, PageQuery pageQuery) {
        return tagsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出标签列表
     */
    @SaCheckPermission("web:tags:export")
    @Log(title = "标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TagsBo bo, HttpServletResponse response) {
        List<TagsVo> list = tagsService.queryList(bo);
        ExcelUtil.exportExcel(list, "标签", TagsVo.class, response);
    }

    /**
     * 获取标签详细信息
     *
     * @param tagId 主键
     */
    @SaCheckPermission("web:tags:query")
    @GetMapping("/{tagId}")
    public R<TagsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long tagId) {
        return R.ok(tagsService.queryById(tagId));
    }

    /**
     * 新增标签
     */
    @SaCheckPermission("web:tags:add")
    @Log(title = "标签", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TagsBo bo) {
        return toAjax(tagsService.insertByBo(bo));
    }

    /**
     * 修改标签
     */
    @SaCheckPermission("web:tags:edit")
    @Log(title = "标签", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TagsBo bo) {
        return toAjax(tagsService.updateByBo(bo));
    }

    /**
     * 删除标签
     *
     * @param tagIds 主键串
     */
    @SaCheckPermission("web:tags:remove")
    @Log(title = "标签", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tagIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] tagIds) {
        return toAjax(tagsService.deleteWithValidByIds(List.of(tagIds), true));
    }

    /**
     * 模糊查询
     */
    @GetMapping("/getListTags")
    public TableDataInfo<TagsVo> getListTags(TagsBo bo, PageQuery pageQuery) {
        return tagsService.getListTags(bo, pageQuery);
    }

}
